/*eslint-disable*/
export const formatGraph = (playInfo) => {
  if (!playInfo || !playInfo.definition) return null;
  const name = playInfo.name;
  const tasks = playInfo.definition[name].tasks;
  const edges = [];

  const nodes = Object.keys(tasks).map(key => {
    let type = 'modelRect';
    let linkPoints = {
      top: true,
      bottom: true,
      left: false,
      right: false,
      fill: '#5E728C',
      stroke: '#5E728C',
    };
    // 处理边
    const next = tasks[key]['on-success'];
    if (next) {
      // 说明是正常流程节点
      if (next.default) {
        next.default.forEach(item => {
          edges.push({
            source: key,
            target: item,
            targetAnchor: 0,
          })
        })
      } else {  // 选择节点
        if (next.y) {
          next.y.forEach(item => {
            edges.push({
              source: key,
              label: 'yes',
              labelCfg: {
                style: {
                  fontSize: 14,
                  fill: '#E7EFFF'
                }
              },
              target: Object.keys(item)[0],
              // 该边连入 source 点的第 0 个 anchorPoint，
              sourceAnchor: 1,
              // 该边连入 target 点的第 0 个 anchorPoint，
              targetAnchor: 0,
            })
          })
        }
        if (next.n) {
          next.n.forEach(item => {
            edges.push({
              source: key,
              label: 'no',
              labelCfg: {
                style: {
                  fontSize: 14,
                  fill: '#E7EFFF'
                }
              },
              target: Object.keys(item)[0],
              // 该边连入 source 点的第 0 个 anchorPoint，
              sourceAnchor: 2,
              // 该边连入 target 点的第 0 个 anchorPoint，
              targetAnchor: 0,
            })
          })
        }
      }
    }

    const anchorPoints = [];
    if (tasks[key].nick === '流程开始') {
      anchorPoints.push([0.5, 1]);
      type = 'ellipse';
      linkPoints.top = false;
    }

    if (tasks[key].nick === '流程结束') {
      anchorPoints.push([0.5, 0]);
      type = 'ellipse';
      linkPoints.bottom = false;
    }

    if (next && next.default) {
      anchorPoints.push([0.5, 0]);
      anchorPoints.push([0.5, 1]);
    }

    if (next && next.y && next.n) {
      anchorPoints.push([0.5, 0]);
      anchorPoints.push([0, 0.5]);
      anchorPoints.push([1, 0.5]);
      type = 'diamond';
      linkPoints.bottom = false;
      linkPoints.left = true;
      linkPoints.right = true;
    }

    //是否是app节点 
    // const integrationReg = /integration=[a-z]*/;
    const appReg = /app="[-_a-zA-Z0-9]*"/;
    const scriptReg = /script="[-_a-zA-Z0-9]*"/;
    const isSwitchReg = /isSwitch=[a-z]*/;
    // const usingReg = /using="[-a-zA-Z0-9]*"/;

    // const integration = tasks[key].action.match(integrationReg) && tasks[key].action.match(integrationReg)[0].replace(/(integration=|")/g, '');
    // const using = tasks[key].action.match(usingReg) && tasks[key].action.match(usingReg)[0].replace(/(using=|")/g, '');
    const app = tasks[key].action.match(appReg) && tasks[key].action.match(appReg)[0].replace(/(app=|")/g, '');
    const script = tasks[key].action.match(scriptReg) && tasks[key].action.match(scriptReg)[0].replace(/(script=|")/g, '');
    const isSwitch = tasks[key].action.match(isSwitchReg) && tasks[key].action.match(isSwitchReg)[0].replace(/(isSwitch=|")/g, '');

    // 处理节点
    return {
      id: key,
      label: tasks[key].nick,
      type,
      size: (tasks[key].nick !== '流程开始' && tasks[key].nick !== '流程结束') && [192, 72],
      x: Number(tasks[key].loc.split(' ')[0]),
      y: Number(tasks[key].loc.split(' ')[1]),
      action: tasks[key].action,
      publish: tasks[key].publish,
      description: tasks[key].description && tasks[key].description.length > 8 ? `${tasks[key].description.slice(0, 8)}...` : tasks[key].description,
      allDescription: tasks[key].description,
      next: next && next.default,
      anchorPoints,
      linkPoints,
      // integration: integration === 'true',
      nodeType: app ? 'app' : script ? 'script' : isSwitch ? 'switch' : '',
      // app,
      // script,
      // isSwitch,
      // using,
    }
  })
  // console.log(nodes);
  // console.log(edges);
  // Object.keys(tasks).forEach(key => {
  //   const next = tasks[key]['on-success'];
  //   if (next) {
  //     // 说明是正常流程节点
  //     if (next.default) {
  //       next.default.forEach(item => {
  //         edges.push({
  //           source: key,
  //           target: item,
  //         })
  //       })
  //     } else {  // 选择节点
  //       if (next.n) {
  //         next.n.forEach(item => {
  //           edges.push({
  //             source: key,
  //             target: Object.keys(item)[0],
  //           })
  //         })
  //       }
  //       if (next.y) {
  //         next.y.forEach(item => {
  //           edges.push({
  //             source: key,
  //             target: Object.keys(item)[0],
  //           })
  //         })
  //       }
  //     }
  //   }
  // })
  return { nodes, edges };
};

export const formatWorkFlow = (playInfo) => {
  if (!playInfo || !playInfo.definition) return null;
  const name = playInfo.name;
  const input = playInfo.definition[name].input;
  const schemas = playInfo.definition[name].schemas;
  const tasks = playInfo.definition[name].tasks;

  const newInput = input.map(key => (
    {
      id: key,
      name: schemas.properties[key].name,
      default: schemas.properties[key].default,
      type: schemas.properties[key].type,
      required: schemas.required.indexOf(key) !== -1,
    }
  ));

  const paramReg = /\$\.[_a-zA-Z0-9]*/g;
  const reg = /=<%\s\$\.[_a-zA-Z0-9]*\s%>/g;
  const outputReg = /\.[_a-zA-Z0-9]*\s/g;
  const appReg = /app="[-_a-zA-Z0-9]*"/;
  const usingReg = /using="[-_a-zA-Z0-9]*"/;
  const integrationReg = /integration=[a-z]*/;
  const scriptReg = /script="[-_a-zA-Z0-9]*"/;
  const isSwitchReg = /isSwitch=[a-z]*/;
  

  // console.log(tasks);
  const newTasks = Object.keys(tasks).map(item => {
    const uuid = item;
    const nodeInfo = tasks[item];
    const nodeInput = nodeInfo.action.match(paramReg);
    const nodeInfoArr = nodeInfo.action.replace(reg, '').split(' ');
    const actionName = nodeInfoArr.shift();
    const integration = nodeInfo.action.match(integrationReg) && nodeInfo.action.match(integrationReg)[0].replace(/(integration=|")/g, '');
    const using = nodeInfo.action.match(usingReg) && nodeInfo.action.match(usingReg)[0].replace(/(using=|")/g, '');
    const app = nodeInfo.action.match(appReg) && nodeInfo.action.match(appReg)[0].replace(/(app=|")/g, '');
    const script = nodeInfo.action.match(scriptReg) && nodeInfo.action.match(scriptReg)[0].replace(/(script=|")/g, '');
    const isSwitch = nodeInfo.action.match(isSwitchReg) && nodeInfo.action.match(isSwitchReg)[0].replace(/(isSwitch=|")/g, '');
    const join = nodeInfo.join;
    const description = nodeInfo.description;
    const loc = nodeInfo.loc;
    const nick = nodeInfo.nick;
    const publish = nodeInfo.publish;

    // 配置节点的输入
    const nodeParam = {};
    if (nodeInput) {
      for (let i = 0; i < nodeInput.length; i++) {
        if (nodeInput[i]) {
          nodeParam[nodeInfoArr[i]] = nodeInput[i].replace('$.', '');
        }
      }
    }



    // 配置节点的输出
    const nodeOutput = {};
    Object.keys(publish).forEach((key) => {
      nodeOutput[key] = publish[key].match(outputReg)[0].replace('.', '');
    })
    return {
      uuid,
      actionName,
      app,
      script,
      description,
      errorStop: undefined,
      input: nodeParam,
      output: nodeOutput,
      integration: integration === 'true',
      nodeType: app ? 'app' : script ? 'script' : isSwitch ? 'switch' : '',
      join,
      using,
      loc,
      nick,
    };
  })

  return { input: newInput, tasks: newTasks };
};
